﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySQL.Db;
using MySql.Data.MySqlClient;

namespace Gollie.Ajax
{
	public class CheckPendingPayment: AjaxResult
	{
		public decimal Amount;
		public short State;
		public UserInfo Customer;

		public CheckPendingPayment() : base(null) { }

		public CheckPendingPayment(AjaxArg arg)
			: base(arg)
		{
		}

		protected override void Init(AjaxArg ajaxArg)
		{
			CheckPendingPaymentArg arg = (CheckPendingPaymentArg)ajaxArg;

			uint uid = Convert.ToUInt32(arg.tokenUser);
			uint vendorId = Convert.ToUInt32(arg.vendorId);

			try
			{
				ResultSet resultSet = Database.Queries.CheckPendingPayment(vendorId, arg.verificationCode);

				Amount = Amount = resultSet.GetDecimal(0, "amount");
				State = resultSet.GetInt16(0, "state");
				int customerId = resultSet.GetInt32(0, "consumerid");
				Customer = UserInfo.GetUserInfo(customerId, true);
			}
			catch (MySqlException mySqlEx)
			{
				this.ErrorCode = mySqlEx.Number;
				this.ErrorMessage = MySqlErrorCodes.GetInstance().GetMessage(mySqlEx);
				logger.Info("CheckPendingPayment failed for userid {0}: {1}", uid, this.ErrorMessage);
			}
			catch (Exception e)
			{
				if (this.ErrorCode == 0)
					this.ErrorCode = 99;

				this.ErrorMessage = e.Message;
				logger.Info("CheckPendingPayment failed for userid {0}: {1}", uid, this.ErrorMessage);
			}
		}
	}
}